<?php

namespace App\Http\Controllers;

//use Illuminate\Http\Request;
use Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Auth;
use App\User; //管理员
use App\Models\Book;
use App\Models\UsersReg;  //会员
use Hash;
use Session;

class AdminUserController extends Controller
{
    // 登录页面
    public function getLogin()
    {
       return View('admin.login');
    }
 
    // 登录操作
    public function postLogin()
    {
      //用 Auth::attempt 来检查输入的内容是否符合数据库里面的记录
      $username = request::input('username');
      $password = request::input('password');
      //获得管理员帐号 保存对象到 Session 中
      Session::put('key', $username);
      if (Auth::attempt(['user_name' => $username, 'password' => $password])) {
        //return Redirect::to('admin/index')
        //->with('message', '成功登录');
        return redirect()->intended('admin/dashboard');;
      } else {
        //return Redirect::to('admin/login')
          //->with('message', '用户名密码不正确')
          //->withInput();
        return '登录没成功';
      }
    }

    // 退出
    public function getLogout()
    {
      //从 Session 中移除对象
      Session::forget('key');
      Auth::logout();
      return Redirect('admin/login');
    }   

    //注册页面
    public function getRegister() {
        return View('admin.register');
    }

    ////注册操作
    public function postRegister() {
        //$input = $Request->only('username', 'email', 'password');
        $User = new User;
        $User->user_name = request::input('username');
        $User->email = request::input('email');
        $User->password = Hash::make(request::input('password'));    
        $User->save();    
        return '注册成功！';
    }

    public function top() {
       //从 Session 取回对象 这里取回管理员帐号名字
       $admin_name = Session::get('key');
       return view('admin.top')->with('admin_name', $admin_name);
    }

    public function menu() {
       return view('admin.menu');
    }

    public function main() 
    {
      $users = UsersReg::count();
      $books = Book::count();
      return view('admin.main',array(
          'users' => $users,
          'books' => $books
      ));
    }
 
}
